/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util;

/**
 * Class {@code Pair} holds two values.
 * @param <E1>   the type of the first value.
 * @param <E2>   the type of the second value.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.Pair">Daniel Léonard</a>
 * @version $Revision$
 */
public class Pair<E1, E2> {

//---------------------------
// Instance variables
//---------------------------

   /** The first value. */
   public E1 first;

   /** The second value. */
   public E2 second;


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    */
   public Pair() {
      this(null, null);
      }

   /**
    * Constructor.
    * @param   first   the first value.
    * @param   second   the second value.
    */
   public Pair(E1 first, E2 second) {
      this.setFirst(first);
      this.setSecond(second);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Changes the first value.
    * @param   first   the new value.
    */
   public void setFirst(E1 first) {
      this.first = first;
      }

   /**
    * Changes the second value.
    * @param   second   the new value.
    */
   public void setSecond(E2 second) {
      this.second = second;
      }

   /**
    * Gets the first value.
    * @return   the value.
    */
   public E1 getFirst() {
      return this.first;
      }

   /**
    * Gets the second value.
    * @return   the value.
    */
   public E2 getSecond() {
      return this.second;
      }


//---------------------------
// Overridden methods from java.lang.Object
//---------------------------

   /**
    * Gets the hashcode of the pair.
    * @return   the hashcode.
    */
   @Override
   public int hashCode() {
      E1 e1 = this.getFirst();
      E2 e2 = this.getSecond();
      int hashcode = 17;
      hashcode = 37 * hashcode + ObjectUtil.hashCode(e1);
      hashcode = 37 * hashcode + ObjectUtil.hashCode(e2);
      return hashcode;
      }

   /**
    * Determines if this pair is equal to the given object. This pair is equal
    * to the given object if the given object is a pair and if their first
    * elements are equals and if their second elements are equals.
    * @param   other   the other object.
    * @return   {@code true} if both object are equal, {@code false} otherwise.
    */
   @Override
   @SuppressWarnings("rawtypes")
   public boolean equals(Object other) {
      boolean evaluation = (other == this);
      if ((false == evaluation) && other instanceof Pair) {
         Pair pair = (Pair) other;

         Object o1, o2;
         evaluation = true;

         o1 = this.getFirst();
         o2 = pair.getFirst();
         evaluation = evaluation && ObjectUtil.areEquals(o1, o2);

         o1 = this.getSecond();
         o2 = pair.getSecond();
         evaluation = evaluation && ObjectUtil.areEquals(o1, o2);
         }
      return evaluation;
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Makes a new {@link Pair} with the given elements.
    * @param   <E1>   the type of the first element.
    * @param   <E2>   the type of the second element.
    * @param   e1   the first element of the pair.
    * @param   e2   the second element of the pair.
    * @return   the new pair.
    */
   public static <E1, E2> Pair<E1, E2> makePair(E1 e1, E2 e2) {
      Pair<E1, E2> pair = new Pair<>(e1, e2);
      return pair;
      }

   }
